from Tkinter import Tk, Button, PanedWindow, Frame, BOTH, Toplevel

class Dialog(Toplevel):

    def __init__(self, parent, title):
        Toplevel.__init__(self, parent)
        self.title(title)
        self.parent = parent
        self.result = None
        self.initial_focus=self.body() # callout
        self.wait_visibility()
        self.grab_set()
        self.protocol('WM_DELETE_WINDOW', self.cancel)
        if not self.initial_focus:
            self.initial_focus = self
        self.initial_focus.focus_set()
        self.wait_window()
        return self.result

    def ok(self):
        if not self.validate(): # callout
            self.initial_focus.focus_set()
            return
        self.withdraw()
        self.update_idletasks()
        try:
            self.result = self.process_result() # callout
        finally:
            self.cancel()

    def cancel(self):
        self.parent.focus_set()
        self.destroy()

    def body(self):
        pass # make your widgets

    def validate(self):
        pass # check your data return True or False

    def process_result(self):
        pass # return self.result

class DynDialog(Dialog):

    def __init__(self, master):
        Dialog.__init__(self, master, 'Dynamic Dialog')

    def body(self):
        f = Frame(self)
        f.pack(expand=True, fill=BOTH)
        pw = PanedWindow(f, showhandle=True)
        pw.pack(expand=True, fill=BOTH)
        left= Frame(pw, bg='green')
        self.buttons = buttons = {}
        for i in range(10):
            Button(left, text=str(i),width=30,command=lambda _p=i:self.select1(_p)).grid(row=i*10)
            buttons[i] = []
            for j in range(5):
                text = '%s-%s' % (i, j)
                buttons[i].append(Button(left, text=text,width=15, command=lambda _p=text:self.select2(_p)))
        right = Frame(pw, bg='yellow')
        pw.add(left, minsize=100)
        pw.add(right, minsize=100)
        return pw

    def select1(self, i):
        done = [w for w in self.buttons[i] if w.winfo_ismapped()]
        for v in self.buttons.values():
            for w in v:
                w.grid_forget()
        if done:
            return
        for idx, w in enumerate(self.buttons[i]):
            w.grid(row=i*10+idx+1)

    def select2(self, ij):
        print ij

def display_dialog():
    print DynDialog(root)

root = Tk()
root.title('Try out a dynamic dialog')
root.geometry('640x480+100+100')
Button(root, text='Press Me', command=display_dialog).pack()
root.mainloop()
